/*****************************************************
** Author:
** Date: 2019-10-19
*
*****************************************************/

#include "comm.h"


/**
 * @brief
 * @param  None
 * @retval None
 */
void print_lua_stack(lua_State* lua, char *str)
{
#ifdef LUA_STACK_DEBUG
    int stackSize, i, t;

    lua_log_inf("==== content of stack from top to bottom ==== %s", str);

    stackSize = lua_gettop(lua);
    for (i = stackSize; i > 0; --i) {
        switch (t = lua_type(lua, i)) {
            case LUA_TNUMBER:
                lua_log_inf("%d [%d] %s: \t%.2f", i, -1 - (stackSize - i), lua_typename(lua, t), lua_tonumber(lua, i));
                break;
            case LUA_TBOOLEAN:
                lua_log_inf("%d [%d] %s: \t%d", i, -1 - (stackSize - i), lua_typename(lua, t), lua_toboolean(lua, i));
                break;
            case LUA_TSTRING:
                lua_log_inf("%d [%d] %s: \t%s", i, -1 - (stackSize - i), lua_typename(lua, t), lua_tostring(lua, i));
                break;
            default:
                // LUA_TTABLE
                // LUA_TTHREAD
                // LUA_TFUNCTION
                // LUA_TLIGHTUSERDATA
                // LUA_TUSERDATA
                // LUA_TNIL
                lua_log_inf("%d [%d] %s", i, -1 - (stackSize - i), lua_typename(lua, t));
                break;
        }
    }
    // psln(stackSize);
#endif
}

void lua_arch_pushtable(lua_State *l, unsigned char *input, int inlen)
{
    lua_newtable(l);
    /*
     *  push number rand(-1) into top stack
     *  lua array index start from array[1]
     *  so we just fill a rand array[0]
     */
    lua_pushinteger(l, -1);     // push a value into stack top
    lua_rawseti(l, -2, 0);      // set array[0] by -1

    int i;
    for (i = 0; i < inlen; ++i) {
        lua_pushinteger(l, input[i]);   // push input[i] into stack
        lua_rawseti(l, -2, i+1);        // set array[i+1] into input[i]
    }
}

void dump_memory(char *str, const unsigned char *buff, unsigned int len)
{
    if(buff == (void *)0 || len == 0)
        return ;

    int idx;
    printf("[lua-linux][DUMP] %s hex %d len:", str, len);
    for(idx = 0; idx < len; idx++)
    {
        if ((idx % 16) == 0)
            printf("\r\n%p:", &buff[idx]);

        printf(" %02x", buff[idx]);
    }
    printf("\r\n");
}

int str2hex(char *str, unsigned char *strhex, int maxsize)
{
    char *p = str;
    int val, cnt = 0, slen = strlen(str);

    if ((slen & 0x01) || ((slen >> 1) > maxsize)) {
        lua_log_err("strlen %d err maxsize %d", slen, maxsize);
        return -1;
    }

    while(*p != '\0') {

        if ((*p >= '0') && (*p <= '9'))
            val = *p - '0';
        else if ((*p >= 'A') && (*p <= 'Z'))
            val = *p - 'A' + 0x0a;
        else if ((*p >= 'a') && (*p <= 'z'))
            val = *p - 'a' + 0x0a;
        else
            val = 0;

        if (cnt & 0x01) // this is low 4 bits
            strhex[cnt>>1] |= val;
        else    // this is high 4 bits
            strhex[cnt>>1] = val << 4;

        p++;
        cnt++;
    }

    return (cnt >> 1);
}

/* End of file */



